<script lang="ts">
  import BasicLayout from "$lib/layout/BasicLayout.svelte";
  import { userStore } from "$lib/stores/user.svelte";
  import LeftMenu from "./LeftMenu.svelte";
  import { listen } from "@tauri-apps/api/event";
  import type { AllNotice } from "$lib/api/notice_type";
  import { goto } from "$app/navigation";

  let { children } = $props();

  $effect(() => {
    userStore.init();
    const unlisten = listen<AllNotice>("notice", (ev) => {
      const payload = ev.payload;
      if (payload.ClientNotice != undefined) {
        if (payload.ClientNotice.WrongSessionNotice != undefined) {
          userStore.userSession = "";
          userStore.userInfo = null;
          userStore.adminUserName = "";
          userStore.adminUserSession = "";
          goto("/");
        }
      }
    });
    return () => {
      unlisten.then((fn) => fn());
    };
  });
</script>

<BasicLayout>
  {#snippet left()}
    <LeftMenu />
  {/snippet}

  {#snippet content()}
    {@render children()}
  {/snippet}
</BasicLayout>
